home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d20 / enet_201.arc / EXTR_NET.C next >
C/C++ Source or Header  |  1992-01-15  |  16KB  |  516 lines

  1. #define TITLE   "EXTR_NET"                /*  Program Name    */
  2. #define DATES   "1988-91"                 /*  Copyright Date  */
  3. #define VERSION "2.01"                    /*  Version Number  */
  4. /*
  5.    
  6.                                   EXTR_NET
  7.                                 Version 2.01
  8.             Copyright (c) Bob Swift, 1988-91.  All Rights Reserved.
  9.              Compiled with the Turbo C++ 1.0 compiler from Borland.
  10.    
  11.    This program is designed to read an input nodelist file and extract
  12.    selected Networks, Regions and/or Zones.  The extracted portions of
  13.    the nodelist are stored in a file specified on the command line.
  14.  
  15.    The format for using this program is as follows:
  16.  
  17.    
  18.         EXTR_NET <srcfile> <destfile> <net1> <net2>...<net10>
  19.    
  20.    where <srcfile>    is the name of the nodelist file to be read
  21.          <destfile>   is the name of the output file created
  22.          <net..>      are the numbers of the Net/Region/Zones to be extracted
  23.                       (the program is presently set to handle 10 numbers).
  24.                       These numbers may be preceded by a N, R or Z in order
  25.                       to extract the entire Net, Region or Zone.  Examples
  26.                       are: 123 N140 r17 Z1
  27.    
  28.  
  29.    The nodelist may be specified with a full drive and path.  The destination
  30.    file must not have a drive or path specified.  It will be output to the 
  31.    current directory.
  32.    
  33.    The program will display a VERY brief set of instructions if it is called
  34.    without any arguments or if it encounters an error.  The following is a
  35.    list of the error codes returned by the program:
  36.    
  37.                   0 - No errors.  Normal termination.
  38.                   1 - Bad or missing command line argument.
  39.                   2 - Unable to open the input file.
  40.                   3 - Unable to open the output file.
  41.                   4 - Problem writing to output file.
  42.                   5 - Problem closing input file.
  43.                   6 - Problem closing output file.
  44.                   7 - Unable to open configuration file.
  45.                   8 - Unable to create backup file.
  46.    
  47.    When an error is encountered, the program will exit immediately and will
  48.    attempt to properly close all files.
  49.    
  50.    Although I have chosen to retain all rights to this program, you are free
  51.    to use it under the following conditions:
  52.    
  53.             - You realize that there is NO Warrantee of any sort.
  54.               It was tested pretty thoroughly here before release
  55.               but who knows what bugs may be lurking within.
  56.             
  57.             - You will not modify the code and release a new version
  58.               of the program.  I welcome suggestions for improvement
  59.               (especially when accompanied by code) but I make no
  60.               guarantee of future releases.
  61.             
  62.             - If you find the program useful, I ask that you do 
  63.               something to brighten somebody else's day.  Just 
  64.               exactly what, I will leave up to you.
  65.  
  66.    
  67.    You may freely distribute this program provided that you distribute only 
  68.    the complete archive which includes the EXTR_NET.EXE, EXTR_NET.C and
  69.    EXTR_NET.DOC.  In addition, You MUST NOT charge for the program nor can
  70.    you charge a copy fee over $4.00 (including the price of the diskette).  
  71.  
  72.    
  73.                                                    Bob Swift (1:140/24)
  74.  
  75.  
  76.     Revision History
  77.     ----------------
  78.  
  79.     1.00    88/11/08    First release version.
  80.  
  81.     1.01    88/11/08    Correct a minor problem with file names and the
  82.                         creation of the output backup file.
  83.  
  84.     1.10    88/11/09    Finally corrected the null pointer and backup bug
  85.                         for good this time.  Someday I will learn to handle
  86.                         strings properly <grin>.  Also cleaned up the error
  87.                         code handling section so that the error messages
  88.                         make sense and included comment lines in output
  89.                         file to show what input file was used and to
  90.                         separate each extracted Net/Region.
  91.  
  92.     2.00    89/05/07    Modified command line inputs slightly to allow
  93.                         extraction of entire Net, Region or Zone by specifying
  94.                         N, R or Z with the number.  Numbers entered with no
  95.                         specifier will be matched to anything but will only
  96.                         extract until the next occurence of a Host, Region or
  97.                         Zone statement.  Also echoed the search criteria to
  98.                         the output file.
  99.  
  100.     2.01    91/12/28    Cleaned up comments in output file.
  101.  
  102. */
  103.  
  104. #include <stdio.h>
  105. #include <string.h> 
  106.  
  107. void helpscrn(int ernum);
  108. void add_period(char filename[]);
  109. void build_bak_name(char filename[]);
  110. int is_in_dir(char *filename);
  111. int del_backup(char *filename);
  112. int linetest(char *linetst1, char *inline);
  113. int make_backup(char *srcfile, char *destfile);
  114. char version[] = {VERSION};  /* Current Version Number */
  115. char title[] = {TITLE};
  116. char dates[] = {DATES};
  117.  
  118. /*******************************************************************/
  119.  
  120. void main(int argc, char *argv[])
  121. {
  122.  
  123. FILE *infile,*outfile;
  124. char flnames[3][40];
  125. char inline[358];
  126. char linetst1[20];
  127. char lintest[31][20];
  128. char linshow[21][20];
  129. int  lineflag[31];
  130. char semi[20];
  131. char spaces[80];
  132.  
  133. int i,j;
  134. int sub1;
  135. int count;
  136. int flag;
  137.  
  138. strcpy(spaces,"                                                                               ");
  139.  
  140. printf("\n\n%s Version %s\n",title,version);
  141. printf("Copyright (c) Bob Swift, %s.  All Rights Reserved.\n",dates);
  142.  
  143. if (argc < 4 || argc > 14) helpscrn(1);
  144. strcpy(flnames[1],strupr(argv[1]));             /*  Input File  */
  145. strcpy(flnames[2],strupr(argv[2]));             /* Output File  */
  146. strcpy(flnames[3],strupr(argv[2]));             /* Backup File  */
  147.  
  148. if (is_in_dir(flnames[2]) == 1)
  149.     helpscrn(7);
  150.  
  151. add_period(flnames[3]);
  152. build_bak_name(flnames[3]);
  153.  
  154. if (del_backup(flnames[3]) == 1)
  155.     helpscrn(8);
  156.  
  157. /*  Exit if output file has an extension of BAK  */
  158. if (strcmp(flnames[2],flnames[3])==0)
  159.    helpscrn(8);
  160.  
  161. if (make_backup(flnames[2],flnames[3]) == 1)
  162.     helpscrn(8);
  163.  
  164. infile=fopen(flnames[1],"rt");
  165. if (infile == NULL)
  166.     helpscrn(2);
  167.  
  168. if ((outfile=fopen(flnames[2],"wt")) == NULL)
  169.     helpscrn(3);
  170.  
  171. count = 3;
  172. sub1 = 0;
  173. while (count < argc)      /*  Set Up Search Strings  */
  174.   {
  175.     strcpy(semi,argv[count]);
  176.     switch(semi[0])
  177.       {
  178.       case 'n': ;
  179.       case 'N': sub1++;
  180.                 strcpy(lintest[sub1],"Host,");
  181.                 strcpy(linshow[sub1],"Net ");
  182.                 lineflag[sub1] = 'N';
  183.                 i = 5;
  184.                 j = 1;
  185.                 while (semi[j] != '\0')
  186.                   {
  187.                   lintest[sub1][i] = semi[j];
  188.                   i++;
  189.                   j++;
  190.                   }
  191.                 lintest[sub1][i] = '\0';
  192.                 strcat(lintest[sub1],",");
  193.                 i = 4;
  194.                 j = 1;
  195.                 while (semi[j] != '\0')
  196.                   {
  197.                   linshow[sub1][i] = semi[j];
  198.                   i++;
  199.                   j++;
  200.                   }
  201.                 linshow[sub1][i] = '\0';
  202.                 break;
  203.       case 'r': ;
  204.       case 'R': sub1++;
  205.                 strcpy(lintest[sub1],"Region,");
  206.                 strcpy(linshow[sub1],"Region ");
  207.                 lineflag[sub1] = 'R';
  208.                 i = 7;
  209.                 j = 1;
  210.                 while (semi[j] != '\0')
  211.                   {
  212.                   lintest[sub1][i] = semi[j];
  213.                   linshow[sub1][i] = semi[j];
  214.                   i++;
  215.                   j++;
  216.                   }
  217.                 lintest[sub1][i] = '\0';
  218.                 linshow[sub1][i] = '\0';
  219.                 strcat(lintest[sub1],",");
  220.                 break;
  221.       case 'z': ;
  222.       case 'Z': sub1++;
  223.                 strcpy(lintest[sub1],"Zone,");
  224.                 strcpy(linshow[sub1],"Zone ");
  225.                 lineflag[sub1] = 'Z';
  226.                 i = 5;
  227.                 j = 1;
  228.                 while (semi[j] != '\0')
  229.                   {
  230.                   lintest[sub1][i] = semi[j];
  231.                   linshow[sub1][i] = semi[j];
  232.                   i++;
  233.                   j++;
  234.                   }
  235.                 lintest[sub1][i] = '\0';
  236.                 linshow[sub1][i] = '\0';
  237.                 strcat(lintest[sub1],",");
  238.                 break;
  239.       default:
  240.           {
  241.             sub1++;
  242.             lineflag[sub1] = 'A';
  243.             strcpy(lintest[sub1],"Zone,");
  244.             strcpy(linshow[sub1],"Zone ");
  245.             strcat(lintest[sub1],argv[count]);
  246.             strcat(linshow[sub1],argv[count]);
  247.             strcat(lintest[sub1],",");
  248.             sub1++;
  249.             lineflag[sub1] = 'A';
  250.             strcpy(lintest[sub1],"Region,");
  251.             strcpy(linshow[sub1],"Region ");
  252.             strcat(lintest[sub1],argv[count]);
  253.             strcat(linshow[sub1],argv[count]);
  254.             strcat(lintest[sub1],",");
  255.             sub1++;
  256.             lineflag[sub1] = 'A';
  257.             strcpy(lintest[sub1],"Host,");
  258.             strcpy(linshow[sub1],"Net ");
  259.             strcat(lintest[sub1],argv[count]);
  260.             strcat(linshow[sub1],argv[count]);
  261.             strcat(lintest[sub1],",");
  262.           }
  263.       }
  264.   count++;
  265.   }
  266. printf("Extracting:   ");    /*  Echo Input To User  */
  267. for (count=1;count<=sub1;count++)
  268. {
  269.   printf("%s\n              ",linshow[count]);
  270. }
  271. printf("\n");
  272.  
  273. /*  Print header to output file  */
  274. if (fputs(";A Extracted Nodelist Segment : 0\n",outfile) == EOF) helpscrn(4);
  275. if (fputs(";A \n;A Extracted with ",outfile) == EOF) helpscrn(4);
  276. if (fputs(title,outfile) == EOF) helpscrn(4);
  277. if (fputs(" Version ",outfile) == EOF) helpscrn(4);
  278. if (fputs(version,outfile) == EOF) helpscrn(4);
  279. if (fputs("\n;A ",outfile) == EOF) helpscrn(4);
  280. if (fputs("Copyright (c) Bob Swift, ",outfile) == EOF) helpscrn(4);
  281. if (fputs(dates,outfile) == EOF) helpscrn(4);
  282. if (fputs(".  All Rights Reserved.",outfile) == EOF) helpscrn(4);
  283. if (fputs("\n;A \n;A Extracted from ",outfile) == EOF) helpscrn(4);
  284. if (fputs(flnames[1],outfile) == EOF) helpscrn(4);
  285. if (fputs("\n;A \n;A Extracting:\n;A ",outfile) == EOF) helpscrn(4);
  286. for (count=1;count<=sub1;count++)
  287.   {
  288.   if(fputs("\n;A                 ",outfile) == EOF) helpscrn(4);
  289.   if(fputs(linshow[count],outfile) == EOF) helpscrn(4);
  290.   }
  291. if (fputs("\n;A \n;\n",outfile) == EOF) helpscrn(4);
  292.  
  293. /*  Initialize  */
  294. count = sub1;
  295. flag = 1;
  296. j = 0;
  297.  
  298. /*  Get line from source file  */
  299. while (fgets(inline,256,infile) != NULL)
  300.   {
  301.     strcpy(semi,";");
  302.     i = linetest(semi,inline);
  303.     strcpy(semi,";\n");
  304.     if ((linetest(semi,inline) == 1 && flag == 0) || i == 0)
  305.     {
  306.     
  307.       if (flag == 0)
  308.       {
  309.         strcpy(semi,"Host");
  310.         i = linetest(semi,inline);
  311.         if (i == 1 && (j == 'N' || j == 'A')) flag = 1;
  312.         if (flag == 1) if (fputs(";\n",outfile) == EOF) helpscrn(4);
  313.       }
  314.     
  315.       if (flag == 0)
  316.       {
  317.         strcpy(semi,"Region");
  318.         i = linetest(semi,inline);
  319.         if (i == 1 && (j == 'N' || j == 'R' || j == 'A')) flag = 1;
  320.         if (flag == 1) if (fputs(";\n",outfile) == EOF) helpscrn(4);
  321.       }
  322.     
  323.     
  324.       if (flag == 0)
  325.       {
  326.         strcpy(semi,"Zone");
  327.         flag = linetest(semi,inline);
  328.         if (flag == 1) if (fputs(";\n",outfile) == EOF) helpscrn(4);
  329.       }
  330.     
  331.       if (flag == 0)
  332.       {
  333.         if (fputs(inline,outfile) == EOF)
  334.            helpscrn(4);
  335.       }
  336.     
  337.       if (flag == 1)
  338.       {
  339.     
  340.         /*  Cycle through the search patterns  */
  341.         for (sub1=1;sub1<=count && linetest(lintest[sub1],inline)==0;sub1++)
  342.         {
  343.             /*  Null Line  */
  344.         }
  345.         
  346.         if (sub1 <= count)
  347.         {
  348.           if (fputs(inline,outfile) == EOF)
  349.              helpscrn(4);
  350.           flag = 0;
  351.           j = lineflag[sub1];
  352.         }
  353.       }
  354.     }
  355.   if (inline[0] == 'Z' || inline[0] == 'R' || (inline[0] == 'H' && inline[2] == 's'))
  356.     {
  357.       inline[strlen(inline)-1] = '\0';
  358.       i = 0;
  359.       for (sub1=0;sub1<=strlen(inline) && i<3;sub1++)
  360.         {
  361.           if (inline[sub1] == ',')
  362.             {
  363.               i++;
  364.               if (i >= 3)
  365.                 inline[sub1] = '\0';
  366.               if (i == 1)
  367.                 inline[sub1] = ' ';
  368.             }
  369.           if (inline[sub1] == '_')
  370.             inline[sub1] = ' ';
  371.         }
  372.       strcat(inline,spaces);
  373.       inline[79] = '\0';
  374.       printf("%s",inline);
  375.       if (inline[0] == 'Z')
  376.         printf("\n");
  377.       else
  378.         printf("\r");
  379.     }
  380.   }
  381. if (fputs(";S End of extracted file.\n;S \n",outfile) == EOF) helpscrn(4);
  382.  
  383. /*  Close the source file  */
  384. if (fclose(infile) != 0)
  385.    helpscrn(5);
  386.  
  387. /*  Close the destination file  */
  388. if (fclose(outfile) != 0)
  389.    helpscrn(6);
  390.  
  391. /*  Thank the user and exit gracefully  */
  392. printf("%s",spaces);
  393. printf("\nExtraction complete.  Thank-you for using %s.\n\n",title);
  394. exit(0);
  395. }
  396. /*******************************************************************/
  397.  
  398.  
  399. int is_in_dir(char *filename)
  400. /*  Check if input file in current directory  */
  401. {
  402. if (strchr(filename,'\\')!=NULL || strchr(filename,':')!=NULL)
  403.    return(1);
  404.  else
  405.    return(0);
  406. }
  407.  
  408.  
  409.  
  410. void add_period(char filename[])
  411. /*  If input file has no extension finish it with a period  */
  412. {
  413. if (strchr(filename,'.')==NULL)
  414.    strcat(filename,".");
  415. }
  416.  
  417. void build_bak_name(char filename[])
  418. /*  Build file name for backup file  */
  419. {
  420. int flag;
  421. for (flag=0;filename[flag]!='.';flag++)
  422.    {
  423.    /*  NULL Line  */
  424.    }
  425. filename[flag] = '\0';
  426. strcat(filename,".BAK");
  427. }
  428.  
  429.  
  430. int del_backup(char *filename)
  431. /*  Check for existing backup file and delete  */
  432. {
  433. FILE *tempfile;
  434. tempfile=fopen(filename,"r");
  435. fclose(tempfile);
  436. if (tempfile != NULL)
  437.    {
  438.    if (unlink(filename) != 0)
  439.       return(1);
  440.    }
  441. return(0);
  442. }
  443.  
  444.  
  445. int make_backup(char *srcfile, char *destfile)
  446. /*  Rename source file to destination file  */
  447. {
  448. FILE *tempfile;
  449. tempfile=fopen(srcfile,"r");
  450. fclose(tempfile);
  451. if (tempfile != NULL)
  452.    {
  453.    if (rename(srcfile,destfile) != 0)
  454.       return(1);
  455.    }
  456. return(0);
  457. }
  458.  
  459.  
  460. int linetest(char *linetst1, char *inline)
  461. /*  This is the routine that actually checks for a match  */
  462. {
  463. int j;
  464. for ( j=0; linetst1[j] == inline[j]; j++ )
  465.     if (linetst1[j+1] == '\0') return(1);
  466. return(0);
  467. }
  468.  
  469.  
  470. void helpscrn(int ernum)
  471. /*  Here are the error messages and VERY brief instructions  */
  472. {
  473. cprintf("\n");
  474. switch (ernum) {
  475.  
  476. case  1 : printf("Bad or missing command line argument\n\n");
  477.           break;
  478.  
  479. case  2 : printf("Unable to open input file\n\n");
  480.           break;
  481.  
  482. case  3 : printf("Unable to open output file\n\n");
  483.           break;
  484.  
  485. case  4 : printf("Problem writing to output file\n\n");
  486.           break;
  487.  
  488. case  5 : printf("Problem closing input file\n\n");
  489.           break;
  490.  
  491. case  6 : printf("Problem closing output file\n\n");
  492.           break;
  493.  
  494. case  7 : printf("Output file not in current directory\n\n");
  495.           break;
  496.  
  497. case  8 : printf("Unable to create backup file\n\n");
  498.           break;
  499.     }
  500. printf("This program is designed to read an input nodelist file and extract");
  501. printf("\nselected Networks, Regions and/or Zones.  The extracted portions ");
  502. printf("of\nthe nodelist are stored in a file specified on the command line");
  503. printf(".\n\nThe format for using this program is as follows:\n\n\n");
  504. printf("     EXTR_NET <srcfile> <destfile> <net1> <net2>...<net10>\n\n");
  505. printf("where <srcfile>    is the name of the nodelist file to be read\n");
  506. printf("      <destfile>   is the name of the output file created\n");
  507. printf("      <net..>      are the numbers of the Net/Region/Zones to be ex");
  508. printf("tracted\n                   (the program is presently set to handle");
  509. printf(" 10 numbers).\n                   These numbers may be preceded by ");
  510. printf("a N, R or Z in order\n                   to extract the entire Net,");
  511. printf(" Region or Zone.  Examples\n                   are: 123 N140 r17 Z1");
  512. printf("\n\n");
  513. exit(ernum);
  514. }
  515.  
  516.